package mapEditor.view.assetEdit
{
	import asset.mapEditorAssetPropertyPanelViewAsset;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.PropertyItemView;
	import mapEditor.vo.AssetVO;
	
	import utils.DisplayUtil;
	
	public class AssetPropertyPanelView extends mapEditorAssetPropertyPanelViewAsset
	{
		private var _itemList:Vector.<PropertyItemView>=new Vector.<PropertyItemView>();
		private var _assetVO:AssetVO;
		
		public function AssetPropertyPanelView()
		{
			setView();
			addEvent();
		}
		
		private function setView():void
		{

		}
		
		private function addEvent():void
		{
			MapEditorManager.instance.addEventListener(MapEditorEvent.ASSET_ITEM_SELECTED, __onAssetItemSelected);
		}
		
		private function removeEvent():void
		{
			MapEditorManager.instance.removeEventListener(MapEditorEvent.ASSET_ITEM_SELECTED, __onAssetItemSelected);
			if(_assetVO)_assetVO.removeEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
		}
		
		private function __onAssetItemSelected(evt:MapEditorEvent):void
		{
			if(_assetVO)
			{
				_assetVO.removeEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
				_assetVO=null;
			}
			
			_assetVO=evt.date as AssetVO;
			_assetVO.addEventListener(MapEditorEvent.ASSET_ITEM_PROPERTY_CHANGE, __onPropertyChange);
			setAsset();
		}
		
		private function __onPropertyChange(evt:MapEditorEvent):void
		{
			setAsset();
		}
		
		private function addItem(item:PropertyItemView):void
		{
			item.x=1;
			item.y=_itemList.length*item.height;
			posPropertyList.addChild(item);
			_itemList.push(item);
		}
		
		private function clearAllItem():void
		{
			while(_itemList && _itemList.length>0)
			{
				var propertyItemView:PropertyItemView=_itemList.shift();
				propertyItemView.removeEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
				propertyItemView.dispose();
				propertyItemView=null;
			}
		}
		
		private function setAsset():void
		{
			clearAllItem();
			
			var propertyItemView:PropertyItemView=new PropertyItemView("ID", _assetVO.id.toString(), 0, false);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("名称", _assetVO.name, 0);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("类名称", _assetVO.className, 0, false);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("注册点X", _assetVO.registerX.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("注册点Y", _assetVO.registerY.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("注册点Z", _assetVO.registerZ.toString(), 1);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			propertyItemView.gotoAndStop(2);
			addItem(propertyItemView);
			propertyItemView=new PropertyItemView("关键词", _assetVO.keywords, 0);
			propertyItemView.addEventListener(MapEditorEvent.PROPERTY_VALUE_CHANGE, __onPropertyItemChange);
			addItem(propertyItemView);
			
			posPropertyList.graphics.clear();
			posPropertyList.graphics.lineStyle(1, 0x6e6e6e);
			posPropertyList.graphics.moveTo(0, 0);
			posPropertyList.graphics.lineTo(0, posPropertyList.height);
			posPropertyList.graphics.lineTo(174, posPropertyList.height-1);
			posPropertyList.graphics.lineTo(174, 0);
			posPropertyList.graphics.endFill();
		}
		
		private function __onPropertyItemChange(evt:Event):void
		{
			var propertyItemView:PropertyItemView=evt.currentTarget as PropertyItemView;
			switch(propertyItemView.propertyName)
			{
				case "名称":
					_assetVO.name=propertyItemView.value;
					break;
				case "注册点X":
					_assetVO.registerX=int(propertyItemView.value);
					break;
				case "注册点Y":
					_assetVO.registerY=int(propertyItemView.value);
					break;
				case "注册点Z":
					_assetVO.registerZ=int(propertyItemView.value);
					break;
				case "关键词":
					_assetVO.keywords=propertyItemView.value;
					break;
			}
			_assetVO.change();
		}
		
		public function dispose():void
		{
			removeEvent();
			_assetVO=null;
			clearAllItem();
			_itemList=null;
			if(parent)parent.removeChild(this);
		}

		public function get assetVO():AssetVO
		{
			return _assetVO;
		}

		public function set assetVO(value:AssetVO):void
		{
			_assetVO = value;
			setAsset();
		}
	}
}